#include "manager.h"
#include "renderer.h"

Manager *mgr;

Manager::Manager(MainWindow *_wnd)
{
    m_Wnd = _wnd;
    m_Demo = new MASGK::CDemoConsole();
    m_Demo->GUIStart();
    mgr = this;
}

QPixmap Manager::Uint2Pixmap(uint** _data)
{
    int width = m_Demo->m_Rasterizer->GetBuffor()->GetWidth();
    int height = m_Demo->m_Rasterizer->GetBuffor()->GetHeight();
    QPixmap *retPixmap = new QPixmap(width, height);
    QImage *img = new QImage(retPixmap->toImage());

    for (int i = 0; i < width; i++)
        for (int j = 0; j < height; j++)
        {
            img->setPixel(i, j, _data[width - 1 - i][height - 1 - j]);
        }

    retPixmap = new QPixmap(QPixmap::fromImage(*img));

    return *retPixmap;
}

void Manager::RenderFrame()
{
    uint** data = m_Demo->Render(m_ActualScene);
    QPixmap actualScene = Uint2Pixmap(data);
    emit RenderNext(actualScene);
}

void renderFrame()
{
    mgr->RenderFrame();
}
